package com.jzo2o.foundations.controller.operation;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;

import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.domain.ServeType;
import com.jzo2o.foundations.model.dto.request.RegionPageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.RegionUpsertReqDTO;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.response.RegionResDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeItemService;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.foundations.service.IServeTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域中项目表 前端控制器
 * </p>
 *
 * @author itcast
 * @since 2023-07-03
 */
@Validated
@RestController("serveController")
@RequestMapping("/operation/serve")
@Api(tags = "运营端 - 区域项目相关接口")
public class ServeController {

    @Autowired
    private IServeService serveService;
    @Autowired
    private IServeItemService serveItemService;
    @Autowired
    private IServeTypeService serveTypeService;

    @GetMapping("/page")
    @ApiOperation("区域项目分页查询")
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        // 1.new Page<>
        Page<Serve> servePage = new Page<>( servePageQueryReqDTO.getPageNo(),servePageQueryReqDTO.getPageSize());
        // 2.查询条件
        LambdaQueryWrapper<Serve> eq = new LambdaQueryWrapper<Serve>().eq(Serve::getRegionId, servePageQueryReqDTO.getRegionId());
        // 3.查询所有数据
        Page<Serve> page = serveService.page(servePage, eq);
        // 4.对查询的数据进行封装
        List<ServeResDTO> collect = page.getRecords().stream().map(item -> {
            ServeResDTO serveResDTO = new ServeResDTO();
            BeanUtil.copyProperties(item, serveResDTO);
//           // LambdaQueryWrapper<ServeItem> queryWrapper = new LambdaQueryWrapper<ServeItem>().eq(ServeItem::getId, item.getServeItemId());
            ServeItem serveItem = serveItemService.getById(item.getServeItemId());
            serveResDTO.setReferencePrice(serveItem.getReferencePrice());
            serveResDTO.setServeItemName(serveItem.getName());
            serveResDTO.setServeTypeId(serveItem.getServeTypeId());
            ServeType serveType = serveTypeService.getById(serveItem.getServeTypeId());
            serveResDTO.setServeTypeName(serveType.getName());
            return serveResDTO;
        }).collect(Collectors.toList());
        for (Serve item : page.getRecords()) {

        }
        PageResult<ServeResDTO> objectPageResult = new PageResult<>();
        objectPageResult.setList(collect);
        objectPageResult.setPages(page.getPages());
        objectPageResult.setTotal(page.getTotal());


return objectPageResult;
    }



}
